package com.itheima;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;

public class ArrayListDemo{
    public static void main(String[] args) {
        // 初始化ArrayList（使用钻石操作符）
        ArrayList<String> list = new ArrayList<>();

        // 元素操作
        list.add("zhangsan");
        list.add(0, "lisi");

        String element = list.get(1);
        System.out.println(element);

        list.set(0, "wangwu");
        list.remove(1);

        list.add("zhangsan");
        list.add("lisi");
        list.add("zhaoliu");

        System.out.println(list);
        System.out.println("---------------------- 遍历方法1: 普通for循环 ------------------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println("---------------------- 遍历方法2: 增强for循环 ------------------------");
        for (String str : list) {
            System.out.println(str);
        }

        System.out.println("---------------------- 遍历方法3: 迭代器遍历 ------------------------");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        System.out.println("---------------------- 遍历方法4: Lambda表达式 ----------------------");

        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String name) {
                System.out.println(name);
            }
        });

        list.forEach(( name)-> System.out.println(name));

        list.forEach(System.out::println);
    }
}


